﻿(function(X){
	function deepClone(obj){
		 if(obj == null || typeof(obj) != 'object')  
		 {
		  return obj;
		 }
			 
		 var temp = new obj.constructor();       
		 for(var key in obj)   
		 {
		  temp[key] = deepClone(obj[key]); 
		 }

		 return temp;  
	}
	 
     var data = {
	 
		_title :  'desige',
		
		create_time : 1331025905764,
		
		state : 2,
		
		sort : '',
		
		id : '',
		
		bgImg : '',
		
		description : 'description',
		
		temp : false,
		
		keyword : 'keyword', 
		
		ext_data: {css:'background:#ff0000;border:1px #dfdfdf solid;', bgImg:'', _title:'desige', description:'description', keyword :'keyword'}, //css
		
		url : '',
		
		items : [

		],
	 };
	 
	 var X = Xwb, T = X.ax.Tpl, Util = X.util, ui = X.ui, Base = ui.Base, doc = document, Box = X.ui.MsgBox;
	 
	 var itemsData = {
		model1 : {
				type : 'model1',
				pid : '',
				id : '',
				temp : true,
				_title : '图文混排',
				_btitle : '图文混排',
				_cs : 'img-txt-s1 box-s1',
				bgcolor : '',
				bdcolor : '',
				color : '',
				container : '<ul></ul>',
				url : '#',
				ctype :['unit1','unit1','unit1','unit1','unit1'],
				children : []
			},		
		model2 : {
				type : 'model2',
				likeType : 'model1',
				pid : '',
				id : '',
				temp : true,
				_title : '图片墙',
				_btitle : '图片墙',
				_cs : 'img-s1 box-s1',
				bgcolor : '',
				bdcolor : '',
				color : '',
				container : '<div class="pic-cont"><div class="pic pic-1 pic-s1"></div><div class="col"></div><div class="col"></div><div class="col"></div></div>',
				url : '#',
				ctype :['unit2','unit3','unit3','unit3','unit4','unit5','unit5','unit6','unit7'],
				children : []
			},
			
		model3 : {
				type : 'model3',
				pid : '',
				likeType : 'model1',
				id : '',
				temp : true,
				_title : '问答',
				_btitle : '问答',
				_cs : 'faq-s1 box-s1',
				bgcolor : '',
				bdcolor : '',
				color : '',
				container : '<div class="faq-cont"></div>',
				url : '#',
				ctype :['unit13','unit14','unit15'],
				children : []
			},	
			
		model4 : {
				type : 'model4',
				likeType : 'model1',
				pid : '',
				id : '',
				temp : true,
				_title : '图文混排2',
				_btitle : '图文混排2',
				_cs : 'img-txt-s2 box-s1',
				bgcolor : '',
				bdcolor : '',
				color : '',
				container : '<div class="img-txt-cont"><div class="item-s1"></div><div class="item-s2"><ul class="img-txt-list-s1"></ul></div><div class="item-s3"><ul class="img-txt-list-s1"></ul></div></div>',
				url : '#',
				ctype :['unit8','unit9','unit9','unit10','unit10','unit10'],
				children : []
			},	
			
			
		model5 : {
				type : 'model5',
				likeType : 'model1',
				pid : '',
				id : '',
				temp : true,
				_title : '图文混排3',
				_btitle : '图文混排3',
				_cs : 'img-txt-s3 box-s1',
				bgcolor : '',
				bdcolor : '',
				color : '',
				container : '<div class="img-txt-cont"><div class="item-s1"></div><div class="item-s2"><ul class="img-txt-list-s1"></ul></div></div>',
				url : '#',
				ctype :['unit11','unit12','unit12','unit12'],
				children : []
			},	
			
		model6 : {
			type : 'model6',
			_title : '头图',
			_btitle : '头图',
			pid : '',
			id : '',
			temp : true,
			operate : 'operate',
			_del : true,
			wp : 'banner',
			container : '',
			img : '/img/custom/custom_w930_h200.jpg',
			url : '#',
			ctype :[],
			children : []
		},	
			

			
		unit1 : {
			type : 'unit1',
			pid:'',
			id : '',
			temp : true,
			insertTag : 'ul',
			operate : 'operate',
			cbgcolor : '',
			color : '',
			_title : 'title',
			img : '/img/custom/custom_w161_h111.jpg',
			url : 'http://www.ziyou.com',
			content:'content'			
		},
		
		unit2 : {
			type : 'unit2',
			pid:'',
			id : '',
			wp : 'communit',
			temp : true,
			insertTag : 'div.pic-s1:eq(0)',
			operate : 'operate',
			_title : 'title',
			img : '/img/custom/custom_w261_h388.jpg',
			url : 'http://www.ziyou.com',
			content:'content'
		},
		
		unit3 : {
			type : 'unit3',
			pid:'',
			id : '',
			wp : 'communit',
			temp : true,
			insertTag : 'div.col:eq(0)',
			operate : 'operate',
			_title : 'title',
			img : '/img/custom/custom_w190_h126.jpg',
			url : 'http://www.ziyou.com',
			content:'content'
		},
		
		unit4 : {
			type : 'unit4',
			pid:'',
			id : '',
			wp : 'communit',
			temp : true,
			insertTag : 'div.col:eq(1)',
			operate : 'operate',
			_title : 'title',
			img : '/img/custom/custom_w272_h182.jpg',
			url : 'http://www.ziyou.com',
			content:'content'
		},
		
		unit5 : {
			type : 'unit5',
			pid:'',
			_cs : 'pic pic-4',
			id : '',
			wp : 'communit',
			temp : true,
			insertTag : 'div.col:eq(1)',
			operate : 'operate',
			_title : 'title',
			img : '/img/custom/custom_w134_h201.jpg',
			url : 'http://www.ziyou.com',
			content:'content'
		},
		
		unit6 : {
			type : 'unit6',
			likeType : 'unit5',
			_cs : 'pic pic-5',
			pid:'',
			id : '',
			wp : 'communit',
			temp : true,
			insertTag : 'div.col:eq(2)',
			operate : 'operate',
			_title : 'title',
			img : '/img/custom/custom_w162_h246.jpg',
			url : 'http://www.ziyou.com',
			content:'content'
		},
		
		unit7 : {
			type : 'unit7',
			likeType : 'unit5',
			_cs : 'pic pic-6',
			pid:'',
			wp : 'communit',
			id : '',
			temp : true,
			insertTag : 'div.col:eq(2)',
			operate : 'operate',
			_title : 'title',
			img : '/img/custom/custom_w162_h137.jpg',
			url : 'http://www.ziyou.com',
			content:'content'
		},
		
		unit8 : {
			type : 'unit8',
			_cs : 'pic pic-6',
			pid:'',
			id : '',
			wp : 'unit1',
			temp : true,
			cbgcolor : '',
			color : '',
			insertTag : 'div.item-s1:eq(0)',
			operate : 'operate',
			_title : 'title',
			img : '/img/custom/custom_w370_h230.jpg',
			url : 'http://www.ziyou.com',
			content:'content'
		},
		
		
		unit9 : {
			type : 'unit9',
			pid:'',
			id : '',
			wp : 'unit1',
			temp : true,
			cbgcolor : '',
			color : '',
			insertTag : 'div.item-s2 ul',
			operate : 'operate',
			_title : 'title',
			img : '/img/custom/custom_w170_h110.jpg',
			url : 'http://www.ziyou.com',
			content:'content'
		},
		
		
		unit10 : {
			type : 'unit10',
			pid:'',
			id : '',
			wp : 'unit1',
			temp : true,
			insertTag : 'div.item-s3 ul',
			cbgcolor : '',
			color : '',			
			operate : 'operate',
			_title : 'title',
			img : '/img/custom/custom_w170_h110.jpg',
			url : 'http://www.ziyou.com',
			content:'content'
		},
		
		unit11 : {
			type : 'unit11',
			pid:'',
			id : '',
			wp : 'unit1',
			temp : true,			
			insertTag : 'div.item-s1',
			operate : 'operate',
			_title : 'title',
			img : '/img/custom/custom_w551_h308.jpg',
			url : 'http://www.ziyou.com',
			content:'content'		
		},
		
		unit12 : {
			type : 'unit12',
			pid:'',
			likeType : 'unit9',
			id : '',
			wp : 'unit1',
			temp : true,
			cbgcolor : '',
			insertTag : 'div.item-s2 ul',
			operate : 'operate',
			_title : 'title',
			img : '/img/custom/custom_w200_h130.jpg',
			url : 'http://www.ziyou.com',
			content:'content'		
		},
		
		unit13 : {
			type : 'unit13',
			pid:'',
			_cs : 'item item-s1',
			win : 'fqWin',
			wp : 'fq',
			id : '',
			temp : true,
			insertTag : 'div.faq-cont',
			operate : 'operate',
			_title : 'title',
			list : [{title:'fq'},{title:'fq1'},{title:'fq2'}],
			img : '/img/custom/large_pic.jpg',
			url : 'http://www.ziyou.com',
			content:'w222222'		
		},
		
		unit14 : {
			type : 'unit14',
			pid:'',
			likeType : 'unit13',
			_cs : 'item item-s2',
			win : 'fqWin',
			wp : 'fq',
			list : [{title:'fq'},{title:'fq'},{title:'fq'}],
			id : '',
			temp : true,
			insertTag : 'div.faq-cont',
			operate : 'operate',
			_title : 'title',
			img : '/img/custom/large_pic.jpg',
			url : 'http://www.ziyou.com',
			content:'unit14'		
		},
		
		unit15 : {
			type : 'unit15',
			pid:'',
			likeType : 'unit13',
			_cs : 'item item-s3',
			win : 'fqWin',
			wp : 'fq',
			list : [{title:'unit16'},{title:'fq'},{title:'fq'}],
			id : '',
			temp : true,
			insertTag : 'div.faq-cont',
			operate : 'operate',
			_title : 'title',
			img : '/img/custom/large_pic.jpg',
			url : 'http://www.ziyou.com',
			content:'unit15'		
		}
		
	 };
	 
	 
	 //内容模板(填充)
	 var template = {
		operate : '<div class="operate-area hidden">'+
							'<div class="operate">'+
								'<a href="#" rel="e:ce,id:{.id},pid:{.pid},ct:{.type}"><span class="ico-edit-s2">编辑</span></a>'+
								'[?._del?<a href="#" rel="e:del,id:{.id},pid:{.pid},ct:{.type}">删除</a>?]'+
							'</div>'+
							'<div class="bg"></div>'+
						'</div>',
									
		model1 : '<div class="{._cs}" id="{.id}" rel="t:{.type},i:{.id}" style="border-color:#{.bdcolor}"><div class="hd" style="background:#{.bgcolor}"><h3 style="color:#{.color}">{._title}</h3>[?.isEdit?<div class="operate"><a class="ico-edit-s1" href="#" rel="e:ch">编辑</a><a class="ico-del-s1" href="#"  rel="e:del">删除</a></div>?]</div><div class="bd">{.container}</div></div>',
		model6 : '<div class="banner" id="{.id}" rel="i:{.id}"><a class="img" href="{.url}"><img alt="" src="{.img}"></a>[?.isEdit?{.operate}?]</div>',
		unit1 : '<li id="{.id}" style="background:#{.cbgcolor}"><div class="pic"><a href="{.url}"><img alt="" src="{.img}"></a></div>'+
						'<div class="cont">'+
						'<p class="title" style="color:#{.color};"><a title="{._title}" href="{.url}" style="color:#{.color};">{._title}</a></p>'+
						'<p class="desc" style="color:#{.color};">{.content}</p>'+
						'</div>'+
						'[?.isEdit?{.operate}?]'+
						'</li>',
		
		unit2 : '<a title="{._title}" class="img" href="{.url}"><img alt="{._title}" src="{.img}"><span><em>{._title}</em><i></i></span></a>[?.isEdit?{.operate}?]',
		unit3 : '<div class="pic pic-2" id="{.id}"> <a class="img" href="{.url}"><img alt="{._title}" src="{.img}"></a>[?.isEdit?{.operate}?]</div>',
		unit4 : '<div class="pic pic-3 pic-s1" id="{.id}"><a title="{._title}" class="img" href="{.url}"><img alt="" src="{.img}"><span><em>{._title}</em><i></i></span></a>[?.isEdit?{.operate}?]</div>',
		unit5 : '<div class="{._cs}" id="{.id}"><a class="img" href="{.url}"><img alt="{._title}" src="{.img}"></a>[?.isEdit?{.operate}?]</div>',
		unit8 : '<div class="pic"><a href="{.url}"><img alt="{._title}" src="{.img}"></a></div><div class="info" style="background:#{.cbgcolor}"><p class="title" style="color:#{.color};"><a title="{._title}" href="{.url}" style="color:#{.color};">{._title}</a></p><p class="desc" style="color:#{.color};">{.content}</div>[?.isEdit?{.operate}?]',
		unit9 : '<li id="{.id}"><div class="pic"><a href="{.url}"><img alt="{._title}" src="{.img}"></a></div><div class="info"  style="background:#{.cbgcolor}"><p class="title" style="color:#{.color};"><a title="{._title}" href="{.url}" style="color:#{.color};">{._title}</a></p><p class="desc" style="color:#{.color};">{.content}</p></div>[?.isEdit?{.operate}?]</li>',		
		unit10 : '<li id="{.id}"><div class="pic pic-s1"><a title="{._title}" class="img" href="{.url}"><img alt="{._title}" src="{.img}"><span><em>{._title}</em><i></i></span></a></div><div class="info"  style="background:#{.cbgcolor}"><p class="title" style="color:#{.color};"><a title="{._title}" href="#" style="color:#{.color};">{._title}</a></p><p class="desc" style="color:#{.color};">{.content}</p></div>[?.isEdit?{.operate}?]</li>',
		unit11 : '<div class="pic"><a href="{.url}" class="img"><img alt="{._title}" src="{.img}"></a><a title="{._title}" href="{.url}" class="title"  style="color:#{.color};"><span  style="color:#{.color};">{._title}</span></a></div><div class="info"  style="background:#{.cbgcolor}"><p class="desc"  style="color:#{.color};">{.content}</p></div>[?.isEdit?{.operate}?]',
		unit13 : '<div class="{._cs}" id="{.id}"><div class="title"><p>{._title}</p><i></i></div><ul></ul>[?.isEdit?{.operate}?]</div>',
		unit13List : '<li><p>{.title}</p><i></i></li>',
		fqItem : ['<div class="form-row">',
                        '<label class="form-field" for="scenic-name">&nbsp;</label>',
                        '<div class="form-cont">',
                            '<input type="text" value="" class="ipt-txt w250" name="scenic-name">',
                            '<a class="del" href="#" rel="e:del">删除</a>',
                        '</div>',
                    '</div>'].join(''),
		changeItem : ['<div class="form-row"><label class="form-field" for="">{.title}&nbsp;</label><div class="form-cont"><input value="{.index}" name="{.id}" type="text"/></div></div>'].join('')
	 };
	 
	 //窗口
	 var winTemp = {
		modelWin : ['<div class="form-box">',
                    '[?.ttitle?<div class="form-row">',
                        '<label for="scenic-name" class="form-field">标题</label>',
                        '<div class="form-cont">',
                           '<input id="title" type="text" class="ipt-txt w250" value="{._title}"/>',
                        '</div>',
                    '</div>?]',
                    '[?.tlink?<div class="form-row">',
                        '<label for="scenic-name" class="form-field">链接</label>',
                        '<div class="form-cont">',
                            '<input id="url" type="text" class="ipt-txt w250"  value="{.url}"/>',
                        '</div>',
                    '</div>?]',
					'[?.cbg?<div class="form-row">',
                        '<label for="scenic-name" class="form-field">背景色</label>',
                        '<div class="form-cont">',
                            '<input type="text" maxlength="6" size="6" id="cbgcolor"  name="color" class="ipt-txt w10"  value="{.cbgcolor}"/>',
                        '</div>',
                    '</div>?]',
					'[?.bg?<div class="form-row">',
                        '<label for="scenic-name" class="form-field">标题背景</label>',
                        '<div class="form-cont">',
                            '<input type="text" maxlength="6" size="6" id="bgcolor"  name="color" class="ipt-txt w10"  value="{.bgcolor}"/>',
                        '</div>',
                    '</div>?]',
					'[?._color?<div class="form-row">',
                        '<label for="scenic-name" class="form-field">文字颜色</label>',
                        '<div class="form-cont">',
                            '<input type="text" maxlength="6" size="6" id="color"  name="color" class="ipt-txt w10"  value="{.color}"/>',
                        '</div>',
                    '</div>?]',
					'[?.bd?<div class="form-row">',
                        '<label for="scenic-name" class="form-field">外框颜色</label>',
                        '<div class="form-cont">',
                            '<input type="text" maxlength="6" size="6" id="bdcolor"  name="color" class="ipt-txt w10"  value="{.bdcolor}"/>',
                        '</div>',
                    '</div>?]',
                    '[?.tupload?<div class="form-row">',
                        '<label for="scenic-name" class="form-field">上传图片</label>',
                        '<div class="form-cont"><form action="" method="post" class="upload-pic" name="form" id="uploadForm" enctype="multipart/form-data">',
                            '<input type="file" id="field" name="file"/>',
                        '</form><input value="{.img}" id="imgVal" class="hidden"></div>',
                    '</div>?]',
                    '[?.tcontent?<div class="form-row">',
                        '<label class="form-field">描述</label>',
                        '<div class="form-cont">',
                            '<textarea class="input-area area-s4" id="content">{.content}</textarea>',
                        '</div>',                        
                    '</div>?]',
					'<p class="form-tips"></p>',
                    '<div class="btn-area">',
                        '<a id="submitBtn" href="#" rel="e:s" class="btn-general highlight"><span>确定</span></a>',
                    '</div>',
                '</div>'].join(''),
		fqWin : ['<div class="form-box">',
                    '[?.ttitle?<div class="form-row">',
                        '<label for="scenic-name" class="form-field">标题</label>',
                        '<div class="form-cont">',
                           '<input id="title" type="text" class="ipt-txt w250" value="{._title}"/><a class="add" href="#" rel="e:add">新增问答</a>',
                        '</div>',
                    '</div>?]',
					'<div class="form-row">',
                        '<label class="form-field" for="scenic-name">问题</label>',
                        '<div class="form-cont">',
                        '</div>',
                    '</div>',
                    '[?.tlink?<div class="form-row">',
                        '<label for="scenic-name" class="form-field">链接</label>',
                        '<div class="form-cont">',
                            '<input id="url" type="text" class="ipt-txt w250"  value="{.url}"/>',
                        '</div>',
                    '</div>?]',
                    '<div class="form-row">',
                        '<div class="form-cont">',
						    '<label for="scenic-name" class="form-field">&nbsp;</label>',
                            '<input type="text" value="" class="ipt-txt w250" name="scenic-name">',
                            '<a class="del" href="#" rel="e:del">删除</a>',
                        '</div>',
                    '</div>',
					'<p class="form-tips"></p>',
                    '<div class="btn-area">',
                        '<a id="submitBtn" href="#" rel="e:s" class="btn-general highlight"><span>确定</span></a>',
                    '</div>',
                '</div>'].join(''),
				
	    chooseModel : ['<div class="form-box">',
							'<div class="form-row">',
								'<label class="form-field" for="">1 头图</label>',
								'<div class="form-cont"><input name="ch" value="model6" type="checkbox"  class="ipt-checkbox" /></div>',
							'</div>',	
							'<div class="form-row">',
								'<label class="form-field" for="">2 图文混排</label>',
								'<div class="form-cont"><input name="ch" value="model1" type="checkbox" class="ipt-checkbox" /></div>',
							'</div>',	
							'<div class="form-row">',
								'<label class="form-field" for="">3 图片墙</label>',
								'<div class="form-cont"><input name="ch" value="model2" type="checkbox" class="ipt-checkbox" /></div>',
							'</div>',
							'<div class="form-row">',
								'<label class="form-field" for="">4 问题</label>',
								'<div class="form-cont"><input name="ch" value="model3" type="checkbox" class="ipt-checkbox" /></div>',
							'</div>',	
							'<div class="form-row">',
								'<label class="form-field" for="">5 图文混排2</label>',
								'<div class="form-cont"><input name="ch" value="model4" type="checkbox" class="ipt-checkbox" /></div>',
							'</div>',
							'<div class="form-row">',
								'<label class="form-field" for="">6 图文混排3</label>',
								'<div class="form-cont"><input name="ch" value="model5" type="checkbox" class="ipt-checkbox" /></div>',
							'</div>',									
					  '</div>'].join(''),
		changeItems : ['<div class="form-box">',
							'{.itemsIndex}',									
					  '</div>'].join(''),
		
		//'1<input name="ch" value="model1" type="checkbox">2<input name="ch" value="model2" type="checkbox">3<input name="ch" value="model3" type="checkbox">4<input name="ch" value="model4" type="checkbox">5<input name="ch" value="model5" type="checkbox">',
		
		unit1: '<div><input name="oldimg" id="oldimg" value="{.oldimg}" class="hidden"><input type="file">标题<input id="content" name="content" value="{.content}"><a href="#" rel="e:s">保存</a></div>'
	 
		,setAll : ['<div class="form-box">',
							'<div class="form-row">',
								'<div class="form-cont">',
									'<label for="scenic-name" class="form-field">标题</label>',
									'<input type="text" value="{._title}" class="ipt-txt w250" name="title" id="title">',
								'</div>',
							'</div>',
							'<div class="form-row">',
								'<div class="form-cont">',
									'<label for="scenic-name" class="form-field">keyword</label>',
									'<input type="text" value="{.ext_data.keyword}" class="ipt-txt w250" name="keyword" id="keyword">',
								'</div>',
							'</div>',	
							'<div class="form-row">',
								'<div class="form-cont">',
									'<label for="scenic-name" class="form-field">描述</label>',
									'<input type="text" value="{.ext_data.description}" class="ipt-txt w250" name="description" id="description">',
								'</div>',
							'</div>',	
							'<div class="form-row">',
								'<label for="scenic-name" class="form-field">背景图片</label>',
								'<div class="form-cont"><form action="" method="post" class="upload-pic" name="form" id="uploadForm" enctype="multipart/form-data">',
									'<input type="file" id="field" name="file"/>',
								'</form><input value="{.ext_data.bgImg}" id="imgVal" class="hidden"><a href="#" rel="e:ci">清除背景</a></div>',								
							'</div>',
							'<div class="form-row">',
								'<div class="form-cont">',
									'<label for="scenic-name" class="form-field">附加样式</label>',
									'<textarea  name="css" id="css" rows="8" cols="35">{.ext_data.css}</textarea>',
								'</div>',
							'</div>',
							'<p class="form-tips"></p>',
							'<div class="btn-area">',
								'<a id="submitBtn" href="#" rel="e:s" class="btn-general highlight"><span>确定</span></a>',
							'</div>',							
					  '</div>'].join('')
	 };
	 
	 //打开窗口参数
	 var winParam = {
		banner : {
			tupload : true,
			tlink : true,
			save : function(e, opt, _class){
				var url = this.jq('#url').val();
				opt.url = url;
				opt.img =  this.jq('#imgVal').val();
				opt.temp = false;			
				var html = _class.getElementHtml(opt, template[opt.likeType || opt.type]);
				_class.replaceWith.call(_class, html, opt, opt.insertTag, opt);
				this.close();
			}
		},
		model : {
			tupload : false,
			tcontent: false,
			ttitle : true,
			tlink : true,
			bg : true,
			bd : true,
			_color : true
		},
		communit : {
			tupload : true,
			tcontent: true,
			ttitle : true,
			tlink : true
		},
		
		unit1 : {
			tupload : true,
			tcontent: true,
			ttitle : true,
			tlink : true,
			cbg : true,
			_color : true,
			myReady : function(opt){
				var self = this;
				this.jq().find('#bgcolor,#color,#bdcolor,#cbgcolor').ColorPicker({
					onBeforeShow: function (v) {
						$(v).data('_input', $(this));
						$(v).css('z-index', self.z+1);
						$(this).ColorPickerSetColor(this.value);
					},
					onChange: function (hsb, hex, rgb, v) {
						$(v).data('_input').val(hex)
					}
				})				
			}
		},
		
		fq : {
			ttitle : true,
			tlink : false,
			del : function(e){
				var $e = $(e.src);
				$e.closest('div.form-row').remove();
			},
			add : function(e){
				var fws = this.jq('div.form-row'), temp = $(template.fqItem);
				temp.insertAfter(fws.last());
				return temp;
			},						
			
			save : function(e, opt, _class){
				var title = Util.trim(this.jq('#title').val());
				opt._title = title;
				var fqs = [];
				this.jq('input[name="scenic-name"]').each(function(i){
					var n = Util.trim(this.value);
					if(n){
						fqs.push({title:n});
					}
				});
				opt.list = fqs;
				opt.temp = false;			
				var html = _class.getElementHtml(opt, template[opt.likeType || opt.type]);
				_class.replaceWith.call(_class, html, opt, opt.insertTag, opt);
				this.close();	
			},
			
			myReady : function(opt){
				if(opt.temp) return;
				var list = opt.list, len = list.length, t;
				for(var i=0;i<len;i++){
					if(i == 0){
						this.jq('input[name="scenic-name"]').val(list[i].title);
					}else{
						t = this.add();
						t.find('input').val(list[i].title);
					}
				}
			}
		}
	 
	 };
	 
	 
	 X.reg('createSpecial', Util.create(Base, {
		
			cid : new Date().getTime(),
			
		    actionMgr : true,
			
			getCid : function(){
				this.cid += 1;
				return this.cid+'';
			},
			
			getItemDataById : function(items, id){
				var has = false, target = '';
				for(var i=0, len = items.length;i<len;i++){
					if(items[i].id == id){
						target = items[i];
						break;
					}
					if(items[i].children && items[i].children.length){
						target = this.getItemDataById(items[i].children , id);
						if(target) break;
					}
				}
				if(target) return target;
			},
			
			onactiontrig : function(e) {
				switch (e.get('e')) {
					case 'ce': //一般上传					
						var id = e.get('id')+'' , item = this.getItemDataById(this.data.items, id), type = item.win || 'modelWin';
						var wTemp = winTemp[type], wp = winParam[item.wp||item.type];					
						this.showWin({wTemp:wTemp,pid:e.get('pid'),id:id}, item, wp);
					break;
					case 'che':
						var wTemp = winTemp[e.get('ct')], id = e.get('id')+'';
						var item = this.getItemDataById(this.data.items, id);
						this.showWin({wTemp:wTemp,pid:e.get('pid'),id:id}, item, wp);
					break;
					case 'save':
						this.save();
					break;
					case 'del':
						this.del(e.get('i'));
					break;
					case 'get':
						this.get(id);
					break;
					case 'addmodel':
						this.showChoose();
					break;
					case 'ch':
						var id = e.get('i')+'', item = this.getItemDataById(this.data.items, id), wp = winParam['model'];
						this.setModel(e, item, wp);
					break;
					case 'change' : 
						var data = this.data, items = data.items, len = items.length, htmls = [], _class = this;
						if(len<2){
							Box.alert('', '模块数太少，不能排序！');
							return;
						}
						if(len>0){
							for(var i=0;i<len;i++)
								htmls.push(T.parse(template.changeItem, {index : i+1, title:items[i]._title||items[i]._btitle, id:items[i].id}));							
						}
						function getItemById(id){
							for(var i=0;i<len;i++){
								if(id == items[i].id){
									return items[i];									
								}								
							}							
						};
						
						this.showChoose({winTemp:winTemp.changeItems,title:'调整位置',itemsIndex:htmls.join(''),save:function(){
							var inputs = this.jq('input'), len = inputs.length, its = new Array(len), self = this;
							for(var i = 0 ;i<len; i++){
								var id = inputs[i].name, index = Util.trim(inputs[i].value);
								if(!index || Number(index)>len || its[Number(index) -1]){
									self.setTips('请正确填写顺序！');
									return false;
								}else{
									var im = getItemById(id);
									its[Number(index) -1] = im;
								}
							}
							_class.data.items = its;
							_class.initData();	
						}});			
					break;
					
					case 'setAll' : 
						var _class = this;
						this.showWin({wTemp:winTemp.setAll}, this.data, {save: function(){
							var inputs = this.jq('input'), css = this.jq('textarea');
							var data = _class.data.ext_data;
							_class.data._title = Util.trim(inputs.eq(0).val());
							data.keyword = Util.trim(inputs.eq(1).val());
							data.description = Util.trim(inputs.eq(2).val());
							data.bgImg = Util.trim(this.jq('#imgVal').val());
							data.css = Util.trim(css.val());
							_class.setExt.call(_class);
							_class.isSave = false;
							this.close();
						},title:'全局设置'});
					break;
					
					case 'pre' :
						if(this.data.items.length){
							var inputs = $('#preview input');
							var d = this.getSaveData();
							inputs.eq(0).val(d[0]);
							inputs.eq(1).val(d[1]);
							inputs.eq(2).val(d[2]);
							$('#preview')[0].submit();
						}
					break;
				}				
			},
			
			//设置页面属性
			setExt : function(){
				document.title = this.data._title;
				var data = this.data.ext_data;
				if(!data) return;
				//设置title
				var addSheet = function () {
					var doc, cssCode;
					if (arguments.length == 1) {
						doc = document;
						cssCode = arguments[0]
					} else if (arguments.length == 2) {
						doc = arguments[0];
						cssCode = arguments[1];
					} else {
						alert("addSheet函数最多接受两个参数!");
					}
					if (! +"\v1") {//增加自动转换透明度功能，用户只需输入W3C的透明样式，它会自动转换成IE的透明滤镜  
						var t = cssCode.match(/opacity:(\d?\.\d+);/);
						if (t != null) {
							cssCode = cssCode.replace(t[0], "filter:alpha(opacity=" + parseFloat(t[1]) * 100 + ")")
						}
					}
					cssCode = cssCode + "\n"; //增加末尾的换行符，方便在firebug下的查看。  
					var headElement = doc.getElementsByTagName("head")[0];
					var styleElements = headElement.getElementsByTagName("style");
					if (styleElements.length == 0) {//如果不存在style元素则创建  
						if (doc.createStyleSheet) {    //ie  
							doc.createStyleSheet();
						} else {
							var tempStyleElement = doc.createElement('style'); //w3c  
							tempStyleElement.setAttribute("type", "text/css");
							headElement.appendChild(tempStyleElement);
						}
					}
					var styleElement = styleElements[0];
					var media = styleElement.getAttribute("media");
					if (media != null && !/screen/.test(media.toLowerCase())) {
						styleElement.setAttribute("media", "screen");
					}
					if (styleElement.styleSheet) {    //ie  
						styleElement.styleSheet.cssText += cssCode;
					} else if (doc.getBoxObjectFor) {
						styleElement.innerHTML += cssCode; //火狐支持直接innerHTML添加样式表字串  
					} else {
						styleElement.appendChild(doc.createTextNode(cssCode))
					}
				}
				
				if(data.bgImg === '' || data.bgImg){
					document.body.style.backgroundImage="url("+data.bgImg+")";
					document.body.style.backgroundRepeat="no-repeat";
				}
				data.css && addSheet('#bodyhtml{'+data.css+'}');
			},
			
			
			setModel : function(e, opt, wp){
				var _class = this, id = e.get('id'), data = opt.temp?{img:opt.img}:opt;
				var box = X.use('Box',$.extend({
					contentHtml : winTemp.modelWin,
					cs : 'win-edit-custom win-fixed',
					title : '栏目设置',
					appendTo : doc.body,
					destroyOnClose : true,
					closeable : true,
					actionMgr : true,
					mask: true,
					onViewReady : function(){
						var self = this;
						this.jq().find('#bgcolor,#color,#bdcolor').ColorPicker({
							onBeforeShow: function (v) {
								$(v).data('_input', $(this));
								$(v).css('z-index', self.z+1);
								$(this).ColorPickerSetColor(this.value);
							},
							onChange: function (hsb, hex, rgb, v) {
								$(v).data('_input').val(hex)
							}
						});
					},
					onactiontrig : function(e) {
						switch (e.get('e')) {
							case 's':
								this.save(e);
							break;
						}
					},
					save : function(e){
						var t = Util.trim(this.jq('#title').val()), url = Util.trim(this.jq('#url').val());
						var colors = this.jq('input[name="color"]');
						for(var i=0;i<colors.length;i++){
							var n = Util.trim(colors.eq(i).val());
							//if(n) 
								opt[colors[i].id] = n;							
						}
						opt.temp = false;
						opt._title = t;
						opt.url = url;
						var html = _class.getElementHtml(opt, template[opt.type]);
						_class.replaceWith.call(_class, html, opt, opt.insertTag);				
						_class.eachItem([opt]);
						_class.replaceWith.call(_class, _class.tempHtml.html(), opt, opt.insertTag);
						_class.tempHtml.html('');
						this.close();
					}
				},data, wp));
				
				box.display(1);
			},
			
			showChoose : function(opt){
				var _class = this, opt = opt ? opt:{};
				var box = X.use('Dlg', Util.extend({
					dlgContentHtml : opt.winTemp || winTemp.chooseModel,
					cs : 'win-edit-custom win-fixed',
					title : '选择模块',
					appendTo : doc.body,
					closeable : true,
					buttons: [{title:'保存',id:'ok', cs: 'btn-s1'}],
					actionMgr : true,
					mask: true,
					setTips : function(msg){
						Box.alert('',msg);						
					},
					onViewReady : function(){
						opt.myready && opt.myready();
					},
					onbuttonclick:function(id){
						if(id == 'ok'){
							if(opt.save){
								return opt.save.call(this);
							}else{
								var models = this.jq('input[type="checkbox"]:checked');
								for(var i=0,len=models.length;i<len;i++){
									_class.addNewModel(models.eq(i).val());
								}
							}							
						}
					}				
				}, opt));
				
				box.display(1);
			},
			
			showWin : function(obj, opt, wp){
				var _class = this, data = opt.temp?{img:opt.img}:opt;
				var box = X.use('Box', $.extend({
					contentHtml : obj.wTemp,
					cs : 'win-edit-custom win-fixed',
					title : '图片标题',
					appendTo : doc.body,
					closeable : true,
					destroyOnClose : true,
					actionMgr : true,
					mask: true,
					onViewReady : function(){
						this.myReady && this.myReady(opt);
						var upload = this.jq('#field'), form = this.jq('#uploadForm'), sub = this.jq('#submitBtn'), self = this;
						if(upload.length)
						var up = Xwb.use('UpoadImage', {
							url:Req.mkUrl('admin/jsApi','upload')
							,fileInput:upload
							,jqForm:form[0]
							,uploadBefore : function(e){//正在上传
								sub.data('lock',true);
							}								
							,uploadAfter: function(d){//上传成功回调
								sub.data('lock',false);
								self.jq('#imgVal').val(d.original_pic.url);
						 }});
						 upload.change(function(){
							up.change($(this).val());
						 });
						 upload.data('upload', up);	
					},
					save: function(e) {
						//判断是否已上传
						var title = this.jq('#title').val();
						var content = this.jq('#content').val();
						opt.content = content;
						opt._title = title;
						opt.img =  this.jq('#imgVal').val();
						opt.temp = false;
						opt.url = this.jq('#url').val();
						var colors = this.jq('input[name="color"]');
						for(var i=0;i<colors.length;i++){
							var n = Util.trim(colors.eq(i).val());
							//if(n) 
								opt[colors[i].id] = n;							
						}
						var html = _class.getElementHtml(opt, template[opt.likeType || opt.type]);
						_class.replaceWith.call(_class, html, opt, _class.jq('#'+opt.pid).find(opt.insertTag));
						this.close();					
					},
					onactiontrig : function(e) {
						switch (e.get('e')) {
							case 's':
								if(!$(e.src).data('lock') && !e.lock())
									this.save(e, opt, _class);
							break;
							case 'del':
								this.del(e);
							break;
							case 'add':
								this.add(e);
							break;
							case 'ci' : 
								this.jq('#imgVal').val('');
								Box.tip('已清除');
							break;
						}
					}				
				}, data, wp));
				
				box.display(1);
			},
			
			isSave : true,
			
			onViewReady : function(){				
				//this.copyData =  this.data.clone(); //原始数据
				//this.addModel('model1');
				//this.addModel('model2');
				//this.addModel('model5');
				//this.addModel('model3');
				this.setExt();
				//this.addModel('model1');
				this.initData();
				var self = this;
				window.onbeforeunload =  function(e){
					if(!self.isSave)
						return '未保存内容,确定要离开吗?';
				}				
			},
			
			initData : function(){
				this.isEdit = true;
				this.appendParent.html('');
				this.createEditHtml();		
				this.appendIn();
			},
			
			addNewModel : function(type){
				this.isSave = false;
				var newModel = this.addModel(type);
				this.isEdit = true;
				this.eachItem([newModel]);
				this.appendIn();
			},
			
			appendIn : function(){
				var html = $(this.tempHtml.html());
				this.appendParent.append(html);	
				this.addHover(html);
				this.tempHtml.html('')
			},
			
			replaceWith : function(html, json, it){
				this.isSave = false;
				var jhtml = $(html), dom = this.jq('#'+json.id);
				if(dom.length){
					this.jq('#'+json.id).replaceWith(jhtml);
					json && this.specialExplained(json, this.jq('#'+json.id));					
				}else{
					it.html(jhtml);
				}
				this.addHover(jhtml);
			},
			
			addHover : function(obj){
				obj.find('.operate-area').parent().hover(function(){
						$(this).find('.operate-area').cssDisplay(1);
					},function(){
						$(this).find('.operate-area').cssDisplay(0);
					});
			},
			
			
			addModel : function(type, toType, ctype){
				//var model = itemsData[type].deepClone();
				var model = deepClone(itemsData[type]);
				toType && (model.type = toType);
				ctype && (model.ctype = ctype)
				if(model){
					model.id = this.getCid();
					for(var i=0;i<model.ctype.length;i++){
					    //var item = itemsData[model.ctype[i]].deepClone();
						var item = deepClone(itemsData[model.ctype[i]]);
						item.id = this.getCid();
						item.pid = model.id;
						model.children.push(item);
					}
				}
				
				this.data.items.push(model);
				return model;				
			},
			
			createEditHtml : function(){		
				var items = this.data.items;
				this.tempHtml = $('<div id="out"></div>');	
				this.eachItem(items);				
			},
			
			eachItem : function(items){
				for(var i=0, len = items.length;i<len;i++){
					this.createByItem(items[i]);
					if(items[i].children && items[i].children.length){
						this.eachItem(items[i].children);
					}
				}
			},
			
			createByItem : function(json){			
				if(json.pid){
					var html = this.getElementHtml(json);				
					this.tempHtml.find('#'+json.pid+' '+json.insertTag).append(html);
				}else{
					this.tempHtml.append(this.getElementHtml(json));
				}
				this.specialExplained(json);
			},
			
			getElementHtml : function(json, hasTemp){
				if(this.isEdit && json.operate && json.operate.indexOf('<') == -1 && template[json.operate]){
					var operate = T.parse(template[json.operate], json);
					return T.parse(hasTemp?hasTemp:(json.likeType?template[json.likeType]:template[json.type]), Util.extend({isEdit:this.isEdit, operate:operate}, json));
				}
				return T.parse(hasTemp?hasTemp:(json.likeType?template[json.likeType]:template[json.type]), Util.extend({isEdit:this.isEdit}, json));
			},
			
			getSpecialExplained : function(json){
				switch(json.type){				
					case 'unit13': 
					case 'unit14':
					case 'unit15':
						var len = json.list.length, listhtml = [];
						for(var i=0;i<len;i++){						
							listhtml.push(T.parse(template.unit13List, json.list[i]));
						}
						return listhtml.join('');
					break;					
				};
			},
			
			specialExplained : function(json, insertTo){
				var html = this.getSpecialExplained(json), parent = '';
				switch(json.type){				
					case 'unit13': 
					case 'unit14':
					case 'unit15':
						parent = insertTo ? insertTo.find('ul') : (this.tempHtml.find('#'+json.pid+' '+json.insertTag).children().last().find('ul'));
						//else this.tempHtml.find('#'+json.pid+' '+json.insertTag).children().last().find('ul').html(html);
					break;					
				};
				
				parent && parent.html(html);
			},
			
			
			getSaveData : function(){
				this.isEdit = false;
				this.createEditHtml();
				var htmls = this.tempHtml.children(), content = {}, items = this.data.items, tempDiv = $('<div></div>');
				for(var i=0;i<htmls.length;i++){
					tempDiv.append(htmls.eq(i));
					content[items[i].id] = {html:tempDiv.html(), json:items[i]};
					tempDiv.html('');
				}
				tempDiv.remove();
				return [this.data._title, $.toJSON(content), this.data.ext_data?$.toJSON(this.data.ext_data):''];
			},
			
			save : function(){
				//if(this.data.items.length){
					var d = this.getSaveData(), self = this;
					this.isEdit = true;
					X.request.saveSpecial(this.sid, d[0], d[1], d[2], function(r){
						if(r.isOk()){
							Box.tipOk('保存成功！');
							self.isSave = true;
						}else{
						
						}
						
					});
				//}
			},
			
			del : function(id){
				var items = this.data.items, self = this;
				Box.confirm('提示','你确认要删除这模块吗？',function(r){
					if(r == 'ok'){
						for(var i=0, len = items.length;i<len;i++){
							if(items[i].id == id){
								items.splice(i, 1);
								self.jq('#'+id).remove();
								break;
							}
						}
						self.isSave = false;
					}
				});
			},
			
			
			get : function(sid){
				var self = this;
				X.request.getSpecial(4, function(r){
					if(r.isOk){
						var data = r.getData(), json = eval('('+data.data+')');
						data.items = self.explained.call(self, json);
						delete data.data;
						data.ext_data = eval('('+data.ext_data+')');
						self.data = data;
						self.initData();
					}
				});
			},
			
			explained : function(json){
				var items = [];
				for(var key in json){
					items.push(json[key].json);
				}
				return items;
			}
	 }));
	$(document).ready(function(){
		var sid = X.util.getAddressParam('sid');
		X.request.getSpecial(sid, function(r){
			if(r.isOk){	
				var data = r.getData(), json = eval('('+data.data+')');
				var items = [];
				for(var key in json){
					items.push(json[key].json);
				}
				data.items = items;
				delete data.data;
				data.ext_data = eval('('+data.ext_data+')');
				if(typeof data.ext_data == 'string' || !data.ext_data) data.ext_data = {};
				X.use('createSpecial', {data:data, view : $('#_body')[0], appendParent: $('#bodyhtml'), sid:sid}).display(1);
			}
		});
		 
	})
})(Xwb)